#include "Defines.hpp"
#include "TheApp.hpp"
#pragma once
class WorkNodeManger {
public:
	WorkNodeManger();
	~WorkNodeManger();
	static WorkNodeManger& GetInstance() {
		static WorkNodeManger instance;
		return instance;
	}
	bool InitWorkPath();
	bool addWorkPath( string name,string path);
	//bool  getExePathFromWorkType(Common::EWorkNodeType type, string& path);

	void AddWorkNodeInfo(string workNodeId, Common::EWorkNodeType type, string path , string commandline, string lastUpdateFlag, string from);
	void ClearWorkNode(string lastUpdateFlag);
	WorkNodeInfo* GetWorkNodeInfo(string workNodeId);
	void SetWorkNodeStatus(string workNodeId, Common::EWorkNodeStatus status);
	void SetWorkNodeStatus(WorkNodeInfo* pNodeInfo, Common::EWorkNodeStatus status);
	void SetWorkNodeState(string workNodeId, Common::EWorkNodeState state);
	void SetWorkNodeState(WorkNodeInfo* pNodeInfo, Common::EWorkNodeState state);
	void SetWorkNodeSocket(string workNodeId, SocketBasePtr pSocket);
	void SetWorkNodeTaskId(string workNodeId, string taskId);

	void SyncWorkNodeListToMaster();

	bool startProcess(string workNodeId, Common::EWorkNodeType workType, string commandline);
	bool sendTaskBeginInitMessage(WorkNodeInfo*pNodeInfo ,std::vector<std::string>& param);
	void InitWorkNodeInfoMap() {  mWorkNodeInfoMap.clear(); };
	bool useRunningProcess(WorkNodeInfo* pWorkNodeInfo);
public:
	//void setWorkNodeStartParam(WorkNodeInfo* pNodeInfo);
	long long getNow();
	bool checkIsTimeOutWorkNode(WorkNodeInfo* pNodeInfo, string message);
	void checkWorkNodeState();

	void killProcess(string workId, Common::EWorkNodeType workType, ProcessHandle handle, string from);
public:
	//send to work message
	bool SendCommandToWorkNode(string workNodeId, Common::ESubCommandID commandId, char* pBuffer);
	bool SendRunningWorkNode(WorkNodeInfo* pWorkNode,string taskId);
	bool BroadcastTaskRunningToWorkNode(string taskId);
	bool BroadcastTaskStopToWorkNode(string taskId, Common::ETaskEndReason reason);
	bool BroadcastWorkNodeStopToWorkNode(string taskId);
	bool SendTaskRunningToWorkNode(string workNodeId, string taskId);
	bool SendWorkNodeStartToWorkNode(string workNodeId);
	bool SendWorkNodeStopToWorkNode(string workNodeId);
	bool SendWorkNodeRegisterRet(string taskId,Master::TaskStartFromCommander* pStarttTaskConfig, Common::ESimOneErrorCode Code, string message, SocketBasePtr pSocket);

protected:
	/*typedef map<Common::EWorkNodeType, string> WorkNodePathConfigMap;
	WorkNodePathConfigMap mWorkNodePathConfigMap;

	typedef map<string, Common::EWorkNodeType> WorkNodeNameTypeMap;
	WorkNodeNameTypeMap mWorkNodeNameTypeMap;*/
	//type system dev value
    // typedef std::map<int, std::map<std::string, std::multimap<std::string, std::string>>> WorkNodePathConfigMap;
	// WorkNodePathConfigMap mWorkNodePathConfigMap;
	WorkNodeInfoMap mWorkNodeInfoMap;
	mutable std::recursive_mutex mWorkNodeInfoMapLock;

	//TrayNodeInfoMap mTrayNodeInfoMap;
	//mutable std::recursive_mutex mTrayNodeInfoMapLock;

	WorkNodeInfoList mWorkNodeInfoDeleteList;
	mutable std::recursive_mutex mWorkNodeInfoDeleteListLock;
	bool mbIsConnect;

};
